//
//  Root_HomeViewController.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/12.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "Root_HomeViewController.h"
#import "QHomeOrderViewModel.h"
#import "QHomeTableViewHeaderView.h"
#import <MJRefresh/MJRefresh.h>
@interface Root_HomeViewController () 
@property (nonatomic, strong)NSMutableArray *viewModelsArray;
@property (nonatomic, weak) IBOutlet QHomeTableViewHeaderView *headerInfoView;
@property (nonatomic, weak) IBOutlet UIView *rejectAlert;
@property (nonatomic, copy) void (^rejectOrder)(void);
@end

@implementation Root_HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    MJWeakSelf
    
   MJRefreshNormalHeader *mjHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf refreshTableView];
    }];
    mjHeader.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    
    [self.tableView setMj_header:mjHeader];
    [self.headerInfoView headerWith:@[] clickSignIn:^(id sender) {
        [ProgressHUD showSuccess:@"点击了上班签到"];
    }];
     
}
- (void)settingModels{
    __weak typeof(self) weakSelf = self;
    self.dataArr = @[].mutableCopy;
    
    for (int i = 0; i < arc4random()%6; i++) {
        [self.dataArr addObject:@""];
    }
    
    [self.viewModelsArray removeAllObjects];
    for (id obj in self.dataArr) {
        QHomeOrderViewModel *viewModel = [[QHomeOrderViewModel alloc] initWithModel:obj];
        viewModel.orderType = arc4random()% 2 ?  OrderType_baoche : OrderType_yueche;
        viewModel.isRecived = arc4random()% 2 ;
        
        [viewModel setAcceptCallBack:^{
            [weakSelf requestAcceptOrder:obj];
        }];
        
        [viewModel setRejectCallBack:^{
             [weakSelf showRejectAlert:obj];
        }];
        
        [self.viewModelsArray addObject:viewModel];
    }
     [self.tableView showEmpty:IMAGENAME_ERROT_HOME_EMPTY title:ERROR_HOME_EMPTY_INFO callback:^BOOL{
         return weakSelf.viewModelsArray.count == 0;
     }];
    
    
    [self.tableView reloadData];
}
// 重写父类方法
- (void)loadData{
    if (!self.viewModelsArray.count) {
       [self.tableView.mj_header beginRefreshing];
    }
    
}
//下拉刷新
- (void)refreshTableView{
    
    [self requestForNewOrder];
}
#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 172;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QHomeOrderViewModel *viewModel = self.viewModelsArray[indexPath.row]; 
    QHomeOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    
    if (viewModel.isRecived) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"ReceivingCell"];
    }
    [viewModel modelWith:cell];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QHomeOrderViewModel *viewModel = self.viewModelsArray[indexPath.row];
   
    [self.navigationController pushViewController:[viewModel nextViewController] animated:YES];
}

//------- network
- (void)requestForNewOrder{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self settingModels];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    });
}



- (void)requestAcceptOrder:(id)order{
    [ProgressHUD show:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [ProgressHUD showSuccess:@"您已成功接单"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView.mj_header beginRefreshing];
        });
    });
}

- (void)requestRejectOrder:(id)order{
    
    [ProgressHUD show:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [ProgressHUD dismiss];
        [self.tableView.mj_header beginRefreshing];
    });
}
- (void)showRejectAlert:(id)order{
    __weak typeof(self) weakSelf = self;
    [self.view addSubview:self.rejectAlert];
    [self.rejectAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    self.rejectOrder = ^{
        [weakSelf requestRejectOrder:order];
    };
}


- (void)dismissRejectAlert{
    [self.rejectAlert removeFromSuperview];
}

- (IBAction)confirmReject:(id)sender{
    if (self.rejectOrder) {
        self.rejectOrder();
    }
    [self dismissRejectAlert];
}

- (IBAction)cancelReject:(id)sender{
    [self dismissRejectAlert];
}


#pragma mark - getter setter


- (NSMutableArray *)viewModelsArray{
    if (_viewModelsArray == nil) {
        _viewModelsArray = [[NSMutableArray alloc] init];
    }
    return _viewModelsArray;
}
@end
